package com.dbq.fast.business.me;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.analysis.EventConstant;
import com.dbq.fast.analysis.YibaAnalysis;
import com.dbq.fast.util.RudenessScreenHelper;

/**
 * Online Custom Service Widget (contains: QQ)
 *
 */

public class OnlineServiceWidget {

    private Activity activity; //the target activity
    private View maskView; //pass mask view
    private int resLayout; //activity root layout id
    private PopupWindow mPopupWindow;
    private View popupView;

    public void init(Activity activity, View maskView, int resLayout){
        this.activity = activity;
        this.maskView = maskView;
        this.resLayout = resLayout;
        initPopupWindow();
    }

    /**
     * 初始化在线帮助底部小弹窗
     */
    public void initPopupWindow() {
        popupView = activity.getLayoutInflater().inflate(resLayout, null);
        adapterPopWindow(popupView);

        mPopupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setAnimationStyle(R.style.anim_menu_bottombar);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(activity.getResources(), (Bitmap) null));

        mPopupWindow.getContentView().setFocusableInTouchMode(true);
        mPopupWindow.getContentView().setFocusable(true);
        mPopupWindow.setOnDismissListener(() -> {
            maskView.setVisibility(View.GONE);
        });
        popupView.findViewById(R.id.qq_msg).setOnClickListener(view -> {
            YibaAnalysis.getInstance().event(App.getContext(), EventConstant.EVENT_CLICK_QQ_HELP);
            joinQQGroup("XvVf2aU0tddnXykt6_q8dZma0rawEE-G");
        });
        popupView.findViewById(R.id.fb_msg).setOnClickListener(view -> {
            Toast.makeText(App.getContext(), "·", Toast.LENGTH_SHORT).show();
        });
    }

    /**
     * 显示
     */
    public void show(){
        if(mPopupWindow != null){
            mPopupWindow.showAtLocation(popupView, Gravity.BOTTOM, 0, 0);
        }
    }

    public void destory(){
        if(mPopupWindow != null){
            mPopupWindow.dismiss();
            mPopupWindow = null;
        }
    }

    private void adapterPopWindow(View popupView) {
        if ("motorola".equals(Build.BRAND) || "Nexus 5".equals(Build.MODEL)) {
            popupView.setPadding(0,
                    (int) RudenessScreenHelper.pt2px(App.getContext(), 2),
                    0,
                    (int) RudenessScreenHelper.pt2px(App.getContext(), 2) + (int) RudenessScreenHelper.dp2px(App.getContext(), 48));
        }
    }

    /****************
     *
     * 发起添加群流程。群号：东八区 Zone8 交流群(575983559) 的 key 为： XvVf2aU0tddnXykt6_q8dZma0rawEE-G
     * 调用 joinQQGroup(XvVf2aU0tddnXykt6_q8dZma0rawEE-G) 即可发起手Q客户端申请加群 东八区 Zone8 交流群(575983559)
     *
     * @param key 由官网生成的key
     * @return 返回true表示呼起手Q成功，返回fals表示呼起失败
     ******************/
    private boolean joinQQGroup(String key) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D" + key));
        // 此Flag可根据具体产品需要自定义，如设置，则在加群界面按返回，返回手Q主界面，不设置，按返回会返回到呼起产品界面    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            activity.startActivity(intent);
            return true;
        } catch (Exception e) {
            // 未安装手Q或安装的版本不支持
//            Toast.makeText(App.getContext(), activity.getString(R.string.no_qq_installed), Toast.LENGTH_SHORT).show();
            return false;
        }
    }

}
